const gameWidth = gameDom.clientWidth;

class Pipe extends person{
    constructor(height,top,dom){
        super(52,height,gameWidth,top,-100,0,dom)
    }

    onMove(){
        if(this.left < -this.width){
            this.dom.remove();
        }
    }
}

function getRandom(min,max){
    return Math.floor(Math.random() * (max - min) + min)
}

class PipePare{
    constructor(){
        this.spaceHeight = 180; //水管的间隔
        this.minHeight = 80;
        this.maxHeight = maxTop - this.minHeight - this.spaceHeight;
        const upHeight = getRandom(this.minHeight,this.maxHeight);

        const upDom = document.createElement("div");
        upDom.className = "pipe up";
        this.upPipe = new Pipe(upHeight, 0, upDom);

        const downHeight = maxTop - upHeight - this.spaceHeight;
        const downDom = document.createElement("div");
        downDom.className = "pipe down";
        this.downPipe = new Pipe(downHeight, maxTop - downHeight, downDom);

        gameDom.appendChild(upDom);
        gameDom.appendChild(downDom);
    }

    get useLess(){
        return this.upPipe.left < -this.upPipe.width;
    }

    move(duration){
        this.upPipe.move(duration);
        this.downPipe.move(duration);
    }
}

class PipePareProducer{
    constructor(){
        this.pairs = [];
        this.timer = null;
        this.tick = 2000;
    }

    startProduce(){
        if(this.timer){
            return;
        }
        this.timer = setInterval(() =>{
            this.pairs.push(new PipePare);
            for (let i = 0; i < this.pairs.length; i++) {
                var pair = this.pairs[i];
                if(pair.useLess){
                    this.pairs.splice(i,1);
                    i--;
                }
                
            }
        },this.tick)
    }

    stopProduce(){
        clearInterval(this.timer);
        this.timer = null;
    }
}
